package com.auction.system.exceptions.handler;

import com.auction.system.exceptions.UnLoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 * @ClassName GlobalExceptionHandler
 * @Author 王伟学
 * @Version 1.0
 **/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 未登录异常处理
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = UnLoginException.class)
    public ModelAndView unLoginHandler(HttpServletRequest req, UnLoginException e) throws Exception {
        ModelAndView mav = new ModelAndView();
        log.error("=========>unLogin异常捕捉成功:",e);
        mav.setViewName("system/unLogin");
        return mav;
    }

    /**
     * 默认异常处理
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        log.error("=========>error异常捕捉成功:",e);
        mav.setViewName("system/error");
        return mav;
    }

}
